﻿$(function () {
    Show();
});
function Show() {
    layui.use(function () {
        var table = layui.table;
        var Num = $("#Num").val();
        var State = $("#State").val();
        console.log(State)
        // 先进行表格的初始渲染
        table.render({
            elem: '#carriertable',  // 表格元素的选择器
            page: {
                limit: 5,
                limits: [5, 8, 10]
            },
            toolbar: '#toolbarDemo',
            defaultToolbar: ['exports'],
            cols: [[
                { type: 'checkbox', fixed: 'left', title: '复选框' },
                { field: 'gualitySchemeNumbering', title: '质检方案编号' },
                { field: 'gualitySchemeName', title: '质检方案名称' },
                {
                    field: 'addTime', title: '添加时间', templet: function (d) {
                        return formatTime(d.addTime); // 调用自定义的时间格式转换函数
                    }
                },
                { field: 'addName', title: '创建人' },
                {
                    field: 'state', title: '状态', templet: function (d) {
                        return ChangeState(d.state); 
                    }
                },
                {
                    field: '', title: '操作', templet: function (d) {
                        return '<a class="layui-btn layui-btn-edit layui-btn-xs" onclick="editData(' + d.gualitySchemeId + ')"><i class="layui-icon layui-icon-edit"></i>查看</a> ' +
                            '<a class="layui-btn layui-btn-danger layui-btn-xs" onclick="deleteData(' + d.gualitySchemeId + ')"><i class="layui-icon layui-icon-delete"></i>删除</a>';
                    }

                }
            ]],
            // 初始时可以先不加载数据，等待 ajax 请求成功后再更新
            data: []
        });
        // 创建表格实例
        $.ajax({
            url: "/GualityScheme/GualitySchemeShow",
            type: "get",
            data: {
                pageindex: 1,
                pagesize: 100,
                gualitySchemeNumbering:Num,
                state: State,
                token: localStorage.getItem("token")
            },
            success: function (data) {
                data = JSON.parse(data);
                console.log(data.data.plist);
                // 在 ajax 请求成功后，使用正确的表格 ID 进行 reload
                table.reload('carriertable', {
                    page: {
                        limit: 5,
                        limits: [5, 8, 10]
                    },
                    data: data.data.plist
                });
            },
            error: function () {
                console.log("请求失败");
            }
        });
        
    });
}
function Add() {
    debugger;
    window.location.href = "/GualityScheme/GualitySchemeAdd";
};
function formatTime(timeStamp) {
    if (timeStamp) {
        var date = new Date(timeStamp);
        var year = date.getFullYear();
        var month = ('0' + (date.getMonth() + 1)).slice(-2);
        var day = ('0' + date.getDate()).slice(-2);
        var hour = ('0' + date.getHours()).slice(-2);
        var minute = ('0' + date.getMinutes()).slice(-2);
        var second = ('0' + date.getSeconds()).slice(-2);
        return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
    }
    return "";
}
function ChangeState(state) {
    debugger
    if (state==true) {
        return "启用"
    }
    else {
        return "禁用"
    }
}
window.deleteData = function (id) {
    console.log(id);
    layer.confirm('确定要删除该条数据吗？', function (index) {
        $.ajax({
            type: "Post",
            url: "/GualityScheme/GualitySchemeDelete",  // 替换为实际的后台删除数据接口地址
            data: { data: JSON.stringify({ gualitySchemeId: id }), token: localStorage.getItem("token") },
            success: function (res) {
                var res = JSON.parse(res)
                if (res.data >= 1) {
                    layer.alert("删除成功");
                    Show();  // 重新加载数据，刷新表格显示
                } else {
                    layer.alert("删除失败");
                }
            },
            error: function () {
                layer.msg('删除失败，请稍后重试', {
                    time: 2000,
                    icon: 2
                });
            }
        });
        layer.close(index);
    });
};
window.editData = function (id) {
    sessionStorage.setItem("gualitySchemeId", id)
    console.log(id)
    window.location.href = "/GualityScheme/GualitySchemeUpdateView";
    
    // 先根据id获取要编辑的数据，发起Ajax请求
    //var form = layui.form;
    //$.ajax({
    //    type: "get",
    //    url: "/GualityProject/GualityProjectModel",  // 替换为实际的后台根据id获取数据的接口地址
    //    data: { gualityProjectId: id, token: localStorage.getItem("token") },
    //    success: function (res) {
    //        var res = JSON.parse(res)
    //        console.log(res)
    //        $(res.data).each(function () {
    //            $('#gualityProjectName').val(this.gualityProjectName)
    //            $('#gualityProjectNumbering').val(this.gualityProjectNumbering)
    //            $('#gualityProjectTool').val(this.gualityProjectTool)
    //            $('#gualityProjectMethod').val(this.gualityProjectMethod)
    //            $('#gualityProjectType').val(this.gualityProjectType)
    //            $('#gualityProjectStandardValue').val(this.gualityProjectStandardValue)
    //            $('#gualityProjectRemark').val(this.gualityProjectRemark)
    //            form.render('select');
    //            obj.gualityProjectId = this.gualityProjectId
    //            obj.gualityProjectName = this.gualityProjectName
    //            obj.gualityProjectNumbering = this.gualityProjectNumbering
    //            obj.gualityProjectTool = this.gualityProjectTool
    //            obj.gualityProjectMethod = this.gualityProjectMethod
    //            obj.gualityProjectType = this.gualityProjectType
    //            obj.gualityProjectStandardValue = this.gualityProjectStandardValue
    //            obj.gualityProjectRemark = this.gualityProjectRemark
    //            debugger
    //        })
    //    },
    //});
}